#
#-----[ SQL ]------------------------------------------
#
ALTER TABLE `phpbb_users` ADD `r_total_q` mediumint(8)  DEFAULT '0' NOT NULL ;
ALTER TABLE `phpbb_users` ADD `r_total_d` mediumint(8)  DEFAULT '0' NOT NULL ;
ALTER TABLE `phpbb_users` ADD `r_per_p` mediumint(8)  DEFAULT '0' NOT NULL ;
ALTER TABLE `phpbb_users` ADD `r_period_t` int(11)  DEFAULT '0' NOT NULL ;
ALTER TABLE `phpbb_users` ADD `r_next_t` int(11)  DEFAULT '0' NOT NULL ;
ALTER TABLE `phpbb_users` ADD `r_cur_c` mediumint(8)  DEFAULT '0' NOT NULL ;

ALTER TABLE `phpbb_groups` ADD `r_total_q` mediumint(8)  DEFAULT '0' NOT NULL ;
ALTER TABLE `phpbb_groups` ADD `r_per_p` mediumint(8)  DEFAULT '0' NOT NULL ;
ALTER TABLE `phpbb_groups` ADD `r_period_t` int(11)  DEFAULT '0' NOT NULL ;
ALTER TABLE `phpbb_groups` ADD `r_combin_k` mediumint(8)  DEFAULT '0' NOT NULL ;

#
#-----[ COPY ]------------------------------------------
#
copy root/admin/admin_dl_statuses.php to admin/admin_dl_statuses.php
copy root/templates/subSilver/admin/admin_dl_statuses.tpl to templates/subSilver/admin/admin_dl_statuses.tpl

#
#-----[ OPEN ]------------------------------------------
#
includes/auth.php

#
#-----[ FIND ]------------------------------------------
#
		$auth_user['auth_mod'] = ( $userdata['session_logged_in'] ) ? auth_check_user(AUTH_MOD, 'auth_mod', $u_access, $is_admin) : 0;

#
#-----[ AFTER, ADD ]------------------------------------------
#
		$auth_user['auth_reg'] = ($userdata['session_logged_in'] ) ? TRUE : 0;
		$auth_user['auth_admin'] = ($is_admin ) ? true : 0;

#
#-----[ OPEN ]------------------------------------------
#
includes/functions.php

#
#-----[ FIND ]------------------------------------------
#
function get_groupdata($group, $force_str = false)
{
	global $db;

	if (!is_numeric($group) || $force_str)
	{
		$group = phpbb_clean_username($group);
	}
	else
	{
		$group = intval($group);
	}

	$sql = "SELECT *
		FROM " . GROUPS_TABLE . " 
		WHERE ";
	$sql .= ( ( is_integer($group) ) ? "group_id = $group" : "group_name = '" .  str_replace("\'", "''", $group) . "'" ) . " AND group_id <> 0";
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Tried obtaining data for a non-existent group', '', __LINE__, __FILE__, $sql);
	}

	// Start Advanced IP Tools Pack MOD
	if ( $db->sql_affectedrows() == 0 )
	{
		message_die(GENERAL_ERROR, 'Group does not exist.');
	}
	// End Advanced IP Tools Pack MOD

	return ( $row = $db->sql_fetchrow($result) ) ? $row : false;
}

function prune_dl_st_prevs_list()
{
	global $db;

	$sql = "DELETE FROM " . DOWNLOAD_STATUSES_TBL . "
		WHERE prevs_until <> 0 AND prevs_until < " . time();
	if (!($result = $db->sql_query($sql)))
	{
		message_die(GENERAL_ERROR, 'Could not access prevslist', '', __LINE__, __FILE__, $sql);
	}
}

function check_prevs_g($user_id)
{
	global $db;


	$sql_in = '';
	$sql = "select group_id
		from ".USER_GROUP_TABLE."
		where user_id=$user_id
		and user_pending=0";
	$result = $db->sql_query($sql);
	while ($row = $db->sql_fetchrow($result) )
	{
		$sql_in .= ( ( $sql_in != '' ) ? ', ' : '' ) . $row['group_id'];
	}
	$db->sql_freeresult($result);
	if ($sql_in!= '')
	{
		$sql = "SELECT * 
			FROM " . DOWNLOAD_STATUSES_TBL . "
			WHERE prevs_group_g IN ($sql_in)";
		if (!($result = $db->sql_query($sql)))
		{
			message_die(GENERAL_ERROR, 'Could not access banlist', '', __LINE__, __FILE__, $sql);
		}
		if ($db->sql_fetchrow($result))
		{
			return true;		
		}
		$db->sql_freeresult($result);
	}

	$sql = "SELECT * 
		FROM " . DOWNLOAD_STATUSES_TBL . "
		WHERE prevs_user_pg =$user_id";
	if (!($result = $db->sql_query($sql))) message_die(GENERAL_ERROR, 'Could not access banlist', '', __LINE__, __FILE__, $sql);
	if ($db->sql_fetchrow($result))	{
		return true;		
	}
	return false;
}

function check_prevs_png($user_id)
{
	global $db;

	$sql = "SELECT * 
		FROM " . DOWNLOAD_STATUSES_TBL . "
		WHERE prevs_user_png =$user_id";
	if (!($result = $db->sql_query($sql))) message_die(GENERAL_ERROR, 'Could not access banlist', '', __LINE__, __FILE__, $sql);

	if ($db->sql_fetchrow($result))	{
		return true;		
	}else{ return false; }
}

#
#-----[ REPLACE WITH ]------------------------------------------
#
function prune_dl_st_prevs_list()
{
	global $db;

	$sql = "DELETE FROM " . DOWNLOAD_STATUSES_TBL . "
		WHERE prevs_until <> 0 AND prevs_until < " . time();
	if (!($result = $db->sql_query($sql)))
	{
		message_die(GENERAL_ERROR, 'Could not access prevslist', '', __LINE__, __FILE__, $sql);
	}

}

function refresh_restriction()
{
	global $db, $userdata;
	$current_time=time();
	$user_id = $userdata['user_id'];

	$sql = "UPDATE phpbb_users SET r_cur_c=0, r_next_t=0 WHERE r_next_t!=0 AND r_next_t<$current_time AND user_id=$user_id";
		if (!($result = $db->sql_query($sql)))
		{
			message_die(GENERAL_ERROR, 'Could not ..', '', __LINE__, __FILE__, $sql);
		}
}


function check_prevs_g($user_id)
{
	global $db, $userdata, $is_auth, $board_config;

	$acces_key=false;
	$in_p_group=false;
	$your_perm='';

	$sql_in = ''; 
	$sql = "select group_id
		from ".USER_GROUP_TABLE."
		where user_id=$user_id
		and user_pending=0";
	$result = $db->sql_query($sql);
	while ($row = $db->sql_fetchrow($result) )
	{
		$sql_in .= ( ( $sql_in != '' ) ? ', ' : '' ) . $row['group_id'];
	}
	$db->sql_freeresult($result);
	if ($sql_in!= '')
	{
		$sql = "SELECT * 
			FROM " . DOWNLOAD_STATUSES_TBL . "
			WHERE prevs_group_g IN ($sql_in)";
		if (!($result = $db->sql_query($sql)))
		{
			message_die(GENERAL_ERROR, 'Could not access perm list', '', __LINE__, __FILE__, $sql);
		}
		if ($db->sql_fetchrow($result))
		{
			$in_p_group=true;
		}
		$db->sql_freeresult($result);
	}

	$sql = "SELECT * 
		FROM " . DOWNLOAD_STATUSES_TBL . "
		WHERE prevs_user_pg =$user_id";
	if (!($result = $db->sql_query($sql))) message_die(GENERAL_ERROR, 'Could not access perm list', '', __LINE__, __FILE__, $sql);

	if ($db->sql_fetchrow($result) || $is_auth[$board_config['dl_statuses_perms']] || $in_p_group)
		{
			if (!$is_auth['auth_admin']) 
			{
				$your_perm = ($userdata['r_total_q']=='-1') ? '<span style="color:blue;font-weight:bold;">  -</span>': '<span style="color:blue;font-weight:bold;">'.$userdata['r_total_q'].'</span>';
				$your_perm = '      '.$your_perm.' <br />';
				$your_perm .= ($userdata['r_per_p']=='-1')? '': '  <span style="color:blue;font-weight:bold;">'. $userdata['r_per_p'].'</span>   <span style="color:blue;font-weight:bold;">'.$userdata['r_period_t'].'</span> <br />' ;

				$your_perm .= ($userdata['r_next_t']=='0')? (''): (($userdata['r_cur_c']<$userdata['r_per_p'])? ('    <span style="color:blue;font-weight:bold;">'.($userdata['r_per_p']-$userdata['r_cur_c']).'</span>'.'   <b>'.create_date('d.m.Y g:i:s', $userdata['r_next_t'], $board_config['board_timezone']).'</b><br>'):('      <b>'.create_date('d.m.Y g:i:s', $userdata['r_next_t'], $board_config['board_timezone']).'</b><br>'));

			}
			if (($userdata['r_total_q']!='0' || $userdata['r_total_q']=='-1') && (($userdata['r_cur_c'] < $userdata['r_per_p'] && ($userdata['r_per_p']>0 && $userdata['r_period_t']>0 )) || $userdata['r_per_p']=='-1') || $is_auth['auth_admin'])
			{
				$acces_key=true;
			}
		}					

	return $r_data=$acces_key.'::'.$your_perm;
}

function check_prevs_png($user_id)
{
	global $db;

	$sql = "SELECT * 
		FROM " . DOWNLOAD_STATUSES_TBL . "
		WHERE prevs_user_png =$user_id";
	if (!($result = $db->sql_query($sql))) message_die(GENERAL_ERROR, 'Could not access perm list', '', __LINE__, __FILE__, $sql);

	if ($db->sql_fetchrow($result))	{
		return true;		
	}else{ return false; }
}

#
#-----[ OPEN ]------------------------------------------
#
includes/functions_post.php

#
#-----[ FIND ]------------------------------------------
#
	add_search_words('single', $post_id, stripslashes($post_message), stripslashes($post_subject));

#
#-----[ AFTER ADD ]------------------------------------------
#
	if ($topic_type_gold >0)
		{
				$user_id=$userdata['user_id'];
				$curent_time=time();
				$sql_r_total_q = ($userdata['r_total_q'] == '-1')? '': 'r_total_q=r_total_q-1,';
				$sql_r_cur_c = ($userdata['r_per_p'] < '1')? '': 'r_cur_c=r_cur_c+1,';
				$sql_r_next_t =($userdata['r_per_p'] < '1')? '': ($userdata['r_cur_c']==0)? 'r_next_t='.$curent_time.'+(r_period_t*86400),':'';

				$sql = "UPDATE " . USERS_TABLE . " SET $sql_r_total_q $sql_r_cur_c $sql_r_next_t r_total_d=r_total_d+1 WHERE user_id=$user_id";
				if (!$db->sql_query($sql))
					{
						message_die(GENERAL_ERROR, 'Error in posting', '', __LINE__, __FILE__, $sql);
					}

		}

#
#-----[ OPEN ]------------------------------------------
#
posting.php

#
#-----[ FIND ]------------------------------------------
#
prune_dl_st_prevs_list();

#
#-----[ AFTER, ADD ]------------------------------------------
#
refresh_restriction();

#
#-----[ FIND ]------------------------------------------
#
//
// Set toggles for various options
//

#
#-----[ BEFORE, ADD ]------------------------------------------
#
$r_data = explode("::", check_prevs_g($userdata['user_id']));
$access= $r_data[0];
$your_perms=$r_data[1];
$blacklist= check_prevs_png($userdata['user_id']);

#
#-----[ FIND ]------------------------------------------
#
				$topic_type_gold = ( ($topic_type_gold != $post_data['topic_type_gold']) && ($is_auth[$board_config['dl_statuses_perms']] || check_prevs_g($userdata['user_id']) && (!check_prevs_png($userdata['user_id']))) ) ? $topic_type_gold : $post_data['topic_type_gold'];

#
#-----[ REPLACE WITH ]------------------------------------------
#
				$topic_type_gold = ( ($topic_type_gold != $post_data['topic_type_gold']) && ($access && !$blacklist) ) ? $topic_type_gold : $post_data['topic_type_gold'];
#
#-----[ FIND ]------------------------------------------
#
	if( ($is_auth[$board_config['dl_statuses_perms']] || check_prevs_g($userdata['user_id'])) && (!check_prevs_png($userdata['user_id'])) )
#
#-----[ REPLACE WITH ]------------------------------------------
#
	if ( $access && !$blacklist )

#
#-----[ FIND ]------------------------------------------
#
		$s_type_gold = ' ' . ': <input type="radio" name="topictypegold" value="0"' . ( ( $post_data['topic_type_gold'] == 0 || $topic_type_gold == 0 ) ? ' checked="checked"' : '' ) . ' /> ' . '' . '&nbsp;&nbsp;' . $s_type_gold;

#
#-----[ REPLACE WITH ]------------------------------------------
#
		$s_type_gold = '<input type="radio" name="topictypegold" value="0"' . ( ( $post_data['topic_type_gold'] == 0 || $topic_type_gold == 0 ) ? ' checked="checked"' : '' ) . ' /> ' . '' . '&nbsp;&nbsp;' . $s_type_gold;

#
#-----[ FIND ]------------------------------------------
#
	'S_TYPE_GOLD' => $s_type_gold,

#
#-----[ AFTER ADD ]------------------------------------------
#
	'YOUR_PERMS' => $your_perms,

#
#-----[ OPEN ]------------------------------------------
#
templates/SubSilver/posting_body.tpl

#
#-----[ FIND ]------------------------------------------
#
		  <!-- BEGIN switch_type_gold -->
		  <tr> 
			<td>{S_TYPE_GOLD}</td>
			<td><span class="gen">{L_TYPE_GOLD}</span></td>
		  </tr>
		  <!-- END switch_type_gold -->


#
#-----[ REPLACE WITH ]------------------------------------------
#
		  <!-- BEGIN switch_type_gold -->
		<fieldset class="fieldset" style="margin: 2px;">
			<legend> </legend>
			<div style="padding: 3px">
			 {S_TYPE_GOLD}
			</div>
			<div style="background-color: #DCE3F5;border: 1px dotted black;">{YOUR_PERMS}</div>
		</fieldset>
		  <!-- END switch_type_gold -->

#
#-----[ OPEN ]------------------------------------------
#
groupcp.php

#
#-----[ FIND ]------------------------------------------
#

					$sql = "INSERT INTO " . USER_GROUP_TABLE . " (user_id, group_id, user_pending)
						VALUES (" . $row['user_id'] . ", $group_id, 0)";
					if ( !$db->sql_query($sql) )
					{
						message_die(GENERAL_ERROR, 'Could not add user to group', '', __LINE__, __FILE__, $sql);
					}
#
#-----[ AFTER ADD ]------------------------------------------
#
					$sql = "SELECT prevs_id
						FROM " . DOWNLOAD_STATUSES_TBL . "
						WHERE prevs_group_g=$group_id";
					if ( !($result = $db->sql_query($sql)) )
					{
						message_die(GENERAL_ERROR, 'Could not get user information', '', __LINE__, __FILE__, $sql);
					}
					if ($db->sql_fetchrow($result))
						{
							$user_id_g= $row['user_id'];

							$this_userdata_g = get_userdata($user_id_g);
							$r_total_q = ($group_info['combination_g']=='0') ? ($group_info['r_total_q']) : (($this_userdata_g['r_total_q']=='-1') ? '-1': ($group_info['r_total_q'] =='-1') ? '-1': 'r_total_q+'.$group_info['r_total_q']);
							$r_per_p = $group_info['r_per_p'];
							$r_period_t = $group_info['r_period_t'];


							$sql = "UPDATE " . USERS_TABLE . " SET r_total_q=$r_total_q, r_per_p=$r_per_p, r_period_t=$r_period_t, r_cur_c=0, r_next_t=0 WHERE user_id=$user_id_g";
							if ( !($result = $db->sql_query($sql)) )
							{
								message_die(GENERAL_ERROR, "Couldn't insert restriction data into users table", "", __LINE__, __FILE__, $sql);
							}
						}
#
#-----[ FIND ]------------------------------------------
#
						$sql = "UPDATE " . USER_GROUP_TABLE . " 
							SET user_pending = 0 
							WHERE user_id IN ($sql_in) 
								AND group_id = $group_id"; 

#
#-----[ AFTER ADD ]------------------------------------------
#
						$sql_a = "SELECT prevs_id
							FROM " . DOWNLOAD_STATUSES_TBL . "
							WHERE prevs_group_g=$group_id";
						if ( !($result_a = $db->sql_query($sql_a)) )
						{
							message_die(GENERAL_ERROR, 'Could not get user information', '', __LINE__, __FILE__, $sql);
						}
						if ($db->sql_fetchrow($result_a))
							{
								for($i = 0; $i < count($members); $i++)
								{
					
									$user_id_g= $members[$i];
						
									$this_userdata_g = get_userdata($user_id_g);
									$r_total_q = ($group_info['combination_g']=='0') ? ($group_info['r_total_q']) : (($this_userdata_g['r_total_q']=='-1') ? '-1': ($group_info['r_total_q'] =='-1') ? '-1': 'r_total_q+'.$group_info['r_total_q']);
									$r_per_p = $group_info['r_per_p'];
									$r_period_t = $group_info['r_period_t'];
						
						
									$sql_b = "UPDATE " . USERS_TABLE . " SET r_total_q=$r_total_q, r_per_p=$r_per_p, r_period_t=$r_period_t, r_cur_c=0, r_next_t=0 WHERE user_id=$user_id_g";
									if ( !($result_b = $db->sql_query($sql_b)) )
									{
										message_die(GENERAL_ERROR, "Couldn't insert restriction data into users table", "", __LINE__, __FILE__, $sql);
									}
								}
					
							}


# 
#-----[ SAVE/CLOSE ALL FILES ]------------------------------------------ 
# 
# EoM